package org.mogu.control;


import org.apache.http.HttpResponse;
import org.apache.http.impl.client.DefaultHttpClient;
import org.mogu.R;
import org.mogu.Welcome;
import org.mogu.staticvalue.Convert;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Weibo15 extends RelativeLayout {
	TextView weibocontent;
//	EditText edittext;
	Button cancelbtn;
//	Button replaybtn;
	Button sendbtn;
	Weibo15 weibocontent10;
	String code;
	String replayType;
	String maincode;
	String father;
	Welcome welcome;
	String check;
	
	
	RadioButton rbtn1;
	RadioButton rbtn2;
	RadioButton rbtn3;
	RadioButton rbtn4;
	RadioGroup rbtng;

	DefaultHttpClient client;
	HttpResponse httpResponse;

	public Weibo15(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public Weibo15(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater.from(context).inflate(R.layout.weibo15, this, true);
		weibocontent = (TextView) findViewById(R.id.weibo10content);
		rbtn1=(RadioButton)findViewById(R.id.radio1);
		rbtn2=(RadioButton)findViewById(R.id.radio2);
		rbtn3=(RadioButton)findViewById(R.id.radio3);
		rbtn4=(RadioButton)findViewById(R.id.radio4);
		rbtng=(RadioGroup)findViewById(R.id.radiogroup);
		rbtn2.setVisibility(View.GONE);
//		edittext = (EditText) findViewById(R.id.weibo10text);
		cancelbtn = (Button) findViewById(R.id.weibo_cancel);
		cancelbtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				welcome.goBack();
			}
		});
		sendbtn = (Button) findViewById(R.id.weibo_ok);
		sendbtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if(rbtng.getCheckedRadioButtonId()==R.id.radio1){
					check="1";
					
				}
				if(rbtng.getCheckedRadioButtonId()==R.id.radio2){
					check="2";
				}
				if(rbtng.getCheckedRadioButtonId()==R.id.radio3){
					check="3";
				}
				if(rbtng.getCheckedRadioButtonId()==R.id.radio4){
					check="4";
				}
				if(rbtng.getCheckedRadioButtonId()==-1){
					Toast.makeText(welcome, "请选择要开通的微博。", 4000).show();
					return;
				}
				new Thread() {
					public void run() {
				try {
					
					String url;
					url = Convert.hosturl + "/AddReply?UserName="
							+ Convert.uname + "&UserPwd=" + Convert.upwd+"&Code="+father+"&MainCode="+maincode+"&Type="+replayType+"&Content=**"+check+"**";
//					url="http://weibo.zxxsbook.com/login?username=11&website=wy";
					Intent intent = new Intent();

	                intent.setAction("android.intent.action.VIEW");

	                Uri content_uri_browsers = Uri.parse(url+Convert.testNum);

	                intent.setData(content_uri_browsers);

	                intent.setClassName("com.android.browser","com.android.browser.BrowserActivity");

	                welcome.startActivity(intent);
//					client = new DefaultHttpClient();
//					
//					
//					HttpGet getMethod = new HttpGet(url+ Convert.testNum);
//
//					httpResponse = client.execute(getMethod);
//
//					if (httpResponse.getStatusLine().getStatusCode() == 200) {
//						// 取出回应字串
//						String strResult;
//						strResult = EntityUtils.toString(httpResponse
//								.getEntity());
//						if ("1".equals(strResult)) {
//							Toast.makeText(welcome, "转发失败，稍后再试。", 4000).show();
//						}
//						if ("0".equals(strResult)) {
//							Toast.makeText(welcome, "转发成功。", 4000).show();
//						}
//					}
				} catch (Exception e1) {
//					e1.printStackTrace();
				}finally{
//					welcome.goBack();
				}
			}}.start();
			welcome.goBack();
			Toast.makeText(welcome, "提交成功", 4000).show();
				
			}
		});
		
	}

	public void setContent(String code,String father, String replayType, String maincode,
			 String content) {
//		weibocontent.setText(content);
		this.code = code;
		this.replayType = replayType;
		this.maincode = maincode;
		this.father=father;
		String[] w=content.split("\\*;\\*");
		try{
			
		
		if(w.length>0){
			String[] ww=w[0].split("\\*");
			if(ww.length>0){
				for(int i=0;i<ww.length;i++){
					if("1".equals(ww[i])){
						rbtn1.setText("新浪微博（重新授权）");
					}
					if("2".equals(ww[i])){
						rbtn2.setText("搜狐微博（重新授权）");
					}
					if("3".equals(ww[i])){
						rbtn3.setText("网易微博（重新授权）");
					}
					if("4".equals(ww[i])){
						rbtn4.setText("腾讯微博（重新授权）");
					}
				}
			}
			ww=w[1].split("\\*");
			if(ww.length>0){
				for(int i=0;i<ww.length;i++){
					if("1".equals(ww[i])){
						rbtn1.setText("新浪微博");
					}
					if("2".equals(ww[i])){
						rbtn2.setText("搜狐微博");
					}
					if("3".equals(ww[i])){
						rbtn3.setText("网易微博");
					}
					if("4".equals(ww[i])){
						rbtn4.setText("腾讯微博");
					}
				}
			}
		}
		}catch (Exception e1) {
			Toast.makeText(welcome, content+":数据不对。", 4000).show();
		}
	}

	

	public void setWeibo15(Weibo15 content, Welcome welcome) {
		weibocontent10 = content;
		this.welcome = welcome;
	}

}
